import librosa.display
import numpy as np
import matplotlib.pyplot as plt
import librosa
import math

print('读取训练信号\n')
for x in range(2):
    y, sr = librosa.load(r'C:\Users\WangCan\PycharmProjects\speech\venv\datasource\s1\S1_'+str(x+1)+'.wav', sr=None)
    y = y.reshape(1, len(y))
    if(x == 0):
        maleSignal = y
    else:
        maleSignal = np.c_[maleSignal, y]
    # y, sr = librosa.load(r'C:\Users\WangCan\PycharmProjects\speech\venv\datasource\s16\S16_' + str(x + 1) + '.wav', sr=None)
    # y = y.reshape(1, len(y))
    # if (x == 0):
    #     femaleSignal = y
    # else:
    #     femaleSignal = np.c_[femaleSignal, y]
'#33592400 38061900'
maleSignal=np.array(maleSignal[0][0:82944])
print(len(maleSignal))
'''', len(femaleSignal[0])'''
# librosa.display.waveplot(y,sr=25000, x_axis='time', offset=0.0, ax=None)
# plt.show()
#截取其中一段
# y=np.array(y[0:36864])
# stft短时傅里叶变换
x =librosa.stft(maleSignal)
# 求最大值
amplitude = np.abs(x)
max = np.max(amplitude, axis=0)
m = amplitude/max
# # 矩阵的拆分
# y, z = np.split(x, [100], 1)
# # 矩阵的转置
# x1 = x.transpose()
# x2 = x1.transpose()
# # istft 短时傅里叶逆变换
# m = librosa.istft(x2)
# print(len(m))
# # 提取幅度和相位
# amplitude = np.abs(x)
# angle = np.angle(x)
# # 对数功率谱和对数幅度谱
# LPS = librosa.power_to_db(Amplitude**2, ref=1.0)
#APS = librosa.amplitude_to_db(Amplitude, ref=1.0)
# amplitude = np.sqrt(librosa.db_to_power(LPS))
# # 角度转换
# angle = angle*1j
# m = amplitude*np.power(math.e, angle)
# z = librosa.istft(m)
# print(len(z))
# librosa.output.write_wav("o.wav", maleSignal, sr=25000)
# librosa.output.write_wav(r".\speech\re.wav", z, sr=25000)

# print(len(S[1]),len(S))
# print(librosa.power_to_db(S ** 2))
# print(S**2)
# array([[-33.293, -27.32 , ..., -33.293, -33.293],
#        [-33.293, -25.723, ..., -33.293, -33.293],
#        ...,
#        [-33.293, -33.293, ..., -33.293, -33.293],
#        [-33.293, -33.293, ..., -33.293, -33.293]], dtype=float32)

# plt.figure()
# plt.subplot(2, 1, 1)
# librosa.display.specshow(S ** 2, sr=sr, y_axis='log')  # 从波形获取功率谱图
# plt.colorbar()
# plt.title('Power spectrogram')
# plt.subplot(2, 1, 2)
# # 相对于峰值功率计算dB, 那么其他的dB都是负的，注意看后边cmp值
# librosa.display.specshow(librosa.power_to_db(S ** 2, ref=np.max),
#                          sr=sr, y_axis='log', x_axis='time')
# plt.colorbar(format='%+2.0f dB')
# plt.title('Log-Power spectrogram')
# plt.set_cmap("autumn")
# plt.tight_layout()
# plt.show()


# import torch
# import torch.nn.functional as F
#
# import librosa
# import numpy as np


# print('读取训练信号\n')
# for x in range(900):
#     y, sr = librosa.load(r'C:\Users\WangCan\PycharmProjects\speech\venv\datasource\s1\S1_'+str(x+1)+'.wav', sr=None)
#     y = y.reshape(1, len(y))
#     if(x == 0):
#         maleSignal = y
#     else:
#         maleSignal = np.c_[maleSignal, y]
#     y, sr = librosa.load(r'C:\Users\WangCan\PycharmProjects\speech\venv\datasource\s16\S16_' + str(x + 1) + '.wav',sr=None)
#     y = y.reshape(1, len(y))
#     if (x == 0):
#         femaleSignal = y
#     else:
#         femaleSignal = np.c_[femaleSignal, y]
# print(len(maleSignal[0]), len(femaleSignal[0]))  #33592400 38061900
# '#256点stft为1帧，33280000/256=130000帧语音'
# maleSignal = np.array(maleSignal[0][0:33280000]).reshape(1, -1)
# femaleSignal = np.array(femaleSignal[0][0:33280000]).reshape(1, -1)
# print(len(maleSignal[0]), len(maleSignal[0]) == len(femaleSignal[0]))

# print("读取测试信号\n")
# for x in range(100):
#     y, sr = librosa.load(r'C:\Users\WangCan\PycharmProjects\speech\venv\datasource\s1\S1_'+str(x+901)+'.wav', sr=None)
#     y = y.reshape(1, len(y))
#     if(x == 0):
#         CMaleSignal = y
#     else:
#         CMaleSignal = np.c_[CMaleSignal, y]
#     y, sr = librosa.load(r'C:\Users\WangCan\PycharmProjects\speech\venv\datasource\s16\S16_' + str(x + 901) + '.wav',sr=None)
#     y = y.reshape(1, len(y))
#     if (x == 0):
#         CFemaleSignal = y
#     else:
#         CFemaleSignal = np.c_[CFemaleSignal, y]
# '#3812600 4250350'
# print(len(CMaleSignal[0]), len(CFemaleSignal[0]))
# '#256点stft为1帧，3788800/256=14800帧语音'
# CMaleSignal = np.array(CMaleSignal[0][0:3788800]).reshape(1, -1)
# CFemaleSignal = np.array(CFemaleSignal[0][0:3788800]).reshape(1, -1)
# print(len(CMaleSignal[0]), len(CMaleSignal[0]) == len(CFemaleSignal[0]))

# class Net(torch.nn.Module):
#     def __init__(self):
#         super(Net,self).__init__()
#         self.net = torch.nn.Sequential(
#             torch.nn.Linear(input, 1024),
#             torch.nn.Sigmoid(),
#             torch.nn.Linear(1024, 2048),
#             torch.nn.ReLU(),
#             torch.nn.Linear(2048, 1024),
#             torch.nn.ReLU(),
#             torch.nn.Linear(1024, 256)
#         )
#     def forward(self,x):
#         x = self.net(x)
#         return  x
#
# net=Net()
# if torch.cuda.is_available():
#     net = net.cuda()
#
# learning_rate = 1e-4
# loss_func = torch.nn.MSELoss()
# optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)
